package cn.fzhui.service;

import cn.fzhui.factory.DaoFactory;
import cn.fzhui.model.Student;
import cn.fzhui.util.DBUtils;
import cn.fzhui.util.PageList;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

public class StudentService {

    /**
     * @return
     */
    public int store(Student student) throws SQLException {
        return DaoFactory.getStudentDao().store(student);
    }

    /**
     * 列表
     */
    public PageList pageList(int currentPage, String searchType, String keyword) throws SQLException {
        return DaoFactory.getStudentDao().pageList(currentPage, searchType, keyword);
    }

    /**
     * 根据ID查询学生
     *
     * @param id
     * @return
     */
    public Student findById(int id) throws SQLException {
        String sql = "SELECT * FROM student WHERE id = ?";
        Object[] params = {
                id
        };
        QueryRunner queryRunner = DBUtils.getQueryRunner();
        Student student = queryRunner.query(sql, new BeanHandler<Student>(Student.class), params);
        return student;
    }

    /**
     * 更新数据
     *
     * @return
     */
    public int update(Student student) throws SQLException {
        return DaoFactory.getStudentDao().update(student);
    }

    /**
     * 根据ID进行删除
     *
     * @param id
     * @return
     */
    public int delete(int id) throws SQLException {
       return DaoFactory.getStudentDao().delete(id);
    }

}
